﻿// Write a program to convert hexadecimal numbers to their decimal representation.

using System;

public class HexToDec
{
    static void Main()
    {
        Console.Write("Enter hexadecimal number (or leave empty for Random): ");
        string hexNumberString = Console.ReadLine();
        if (string.IsNullOrEmpty(hexNumberString))
        {
            Random random = new Random();
            int decimalNumber = random.Next(int.MinValue, int.MaxValue);
            hexNumberString = decimalNumber.ToString("x").ToUpper();
            Console.WriteLine("Generated Random numbers is: {0}", hexNumberString);
        }

        Console.WriteLine("Decimal representation is : {0}", ToDecimal(hexNumberString));
        Console.WriteLine("Decimal number for control: {0}", int.Parse(hexNumberString, System.Globalization.NumberStyles.HexNumber));
    }

    public static int ToDecimal(string hexNumberString)
    {
        int sum = 0;
        char[] hexNumberArray = hexNumberString.ToCharArray();
        for (int i = hexNumberArray.Length - 1, power = 0; i >= 0; i--, power++)
        {
            sum += DecToHex.ToNumber(hexNumberArray[i]) * BinToDec.Pow(16, power);
        }

        return sum;
    }
}